package io.zhengsh.juc._2synchronized1.lightweightlock;

//import lombok.SneakyThrows;
// import lombok.extern.slf4j.Slf4j;
import lombok.SneakyThrows;
import org.openjdk.jol.info.ClassLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//@Slf4j
public class LightweightLockTest {

    private static Logger log = LoggerFactory.getLogger(LightweightLockTest.class);

    public static void main(String[] args) throws InterruptedException {
        log.debug(ClassLayout.parseInstance(new Object()).toPrintable());
        Thread.sleep(5000);
        Object obj1 = new Object();
        log.debug(ClassLayout.parseInstance(obj1).toPrintable());

        ExecutorService executeService = Executors.newFixedThreadPool(2);
        executeService.submit(new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                log.debug("start ... \r\n" + ClassLayout.parseInstance(obj1).toPrintable());
                synchronized (obj1) {
                    log.debug("lock ... \r\n" + ClassLayout.parseInstance(obj1).toPrintable());
                }
                log.debug("unlock ... \r\n" + ClassLayout.parseInstance(obj1).toPrintable());
            }
        }, "thread0"));

        //Thread.sleep(1000);

        executeService.submit(new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                log.debug("start ... \r\n" + ClassLayout.parseInstance(obj1).toPrintable());
                synchronized (obj1) {
                    log.debug("lock ... \r\n" + ClassLayout.parseInstance(obj1).toPrintable());
                }
                log.debug("unlock ... \r\n" + ClassLayout.parseInstance(obj1).toPrintable());
            }
        }, "thread1"));

        Thread.sleep(5000);
        log.debug(ClassLayout.parseInstance(obj1).toPrintable());

        executeService.shutdown();

    }

}
